<script setup lang="ts">
import { ref } from 'vue'
import { TabBar, SearchBar, BookmarkBar, Start } from './components'
import useBookmarks from './use-bookmarks'

const SSO_HOST = 'https://open.feishu.cn/open-apis/authen/v1/index?app_id=cli_a519551272f8d00e'
const authed = ref(false)
const { setBookmarks } = useBookmarks()

// FIXME:
const url = new URL(window.location.href)
const code = url.searchParams.get('code')
if (code) {
  window.ipc.sendWinStartLogin(code)
  window.ipc.onWinDidLogin((platforms) => {
    authed.value = true
    setBookmarks(platforms)
  })
  window.ipc.onWinDidLogout(() => {
    authed.value = false
    ssoLogin()
  })
} else {
  ssoLogin()
}
function ssoLogin() {
  const appUrl = window.location.origin + window.location.pathname
  window.location.href = `${SSO_HOST}&redirect_uri=${appUrl}`
}
</script>

<template>
  <div class="home">
    <template v-if="authed">
      <TabBar></TabBar>
      <SearchBar></SearchBar>
      <BookmarkBar></BookmarkBar>
      <Start />
    </template>
  </div>
</template>

<style scoped>
.home {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
</style>
